<?php

namespace app\common\validate;

use think\Validate;

class User extends Validate
{

    /**
     * 验证规则
     */
    protected $rule = [
        'id' => 'require',
        'nickname' => 'require',
        'phone' => 'uniquePhone',
        'type' => 'require',
    ];

    /**
     * 提示消息
     */
    protected $message = [
        "id.require" => "缺少参数id",
        "nickname.require" => "请输入昵称",
//        "phone.require" => "请输入手机号码",
        "phone.uniqueTitle" => "手机号码已存在",
        "type.require" => "请选择用户类型",
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add' => ['nickname', 'phone', 'type'],
        'update' => ['id', 'nickname', 'phone', 'type'],
    ];

    public function uniquePhone($value, $rule, $row)
    {
        $model = new \app\common\model\User();
        $query = $model->where("phone", $value);
        if (!empty($row['id'])) $query->where("id", "<>", $row['id']);
        $findUser = $query->find();
        if (empty($findUser)) return true;
        return false;
    }

}
